// import login from '../login'

const useTestHost = false
const host = useTestHost ? 'https://yzjd.ngrok.jintuandui.cn' : 'https://yzjd.ngrok.jintuandui.cn'

const httpHandler = {
  useMock: true,
  // 处理get请求
  handleGetReq() {
    wx.showLoading({ title: '加载中...' })
    return new Promise(resolve => {
      // todo 模拟操作，后续删除
      setTimeout(() => {
        resolve({ code: 0 })
        wx.hideLoading()
      }, 500)
    })
  },
  // 处理post请求
  handlePostReq(params) {
    wx.showLoading({ title: '加载中...' })
    if (this.useMock) {
      return new Promise(resolve => {
        // todo 模拟操作，后续删除
        setTimeout(() => {
          resolve({ code: 0 })
          wx.hideLoading()
        }, 500)
      })
    } else {
      return new Promise((resolve, reject) => {
        console.log(`params is ${params.url}`)
        params.url = `${host}${params.url}`
        params.method = 'POST'
        wx.request({
          ...params, 
          success: res => {
            resolve(res)
            this.success()
            wx.hideLoading()
          }, 
          fail: err => {
            reject(err)
            this.fail()
            wx.hideLoading()
          }
        })
      })
    }
  },
  success(title = '请求成功') {
    wx.showToast({
      title: title,
      icon: 'success',
      duration: 1500
    })
  },
  fail(title = '请求失败') {
    wx.showToast({
      title: title,
      icon: 'none',
      duration: 1500
    })
  }
}

export default httpHandler
